package cn.flashk.controls.skin.sourceSkin {
	import com.skins.ImgSkinLoader;
	import com.skins.Skin;
	
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.utils.setTimeout;
	
	import cn.flashk.controls.VScrollBar;
	import cn.flashk.controls.managers.SkinLoader;
	import cn.flashk.controls.managers.SourceSkinLinkDefine;
	import cn.flashk.controls.support.UIComponent;

	public class VScrollBarLoaderSkin extends SourceSkin {
		public var arrowUp:Sprite;
		public var arrowDown:Sprite;
		public var bar:Sprite;
		public var scroller:Sprite;
		private var scrol:ImgSkinLoader;
		private var mid:ImgSkinLoader;
		protected var tar:VScrollBar;
		
		protected var lastY:Number = -50;
		protected var needChange:Boolean = false;
		protected var count:uint = 0

		public function VScrollBarLoaderSkin(skinID:String=""){
			super(skinID);
			arrowUp = new Sprite();
			arrowDown = new Sprite();
			bar = new Sprite();
			scroller = new Sprite();
			arrowUp.addChild(Skin.librarys.getSkin(_skinID,"up"));
			arrowDown.addChild(Skin.librarys.getSkin(_skinID,"down"));
			scrol = Skin.librarys.getSkin(_skinID,"scroller") as ImgSkinLoader;
			scrol.x = 1;
			scroller.addChild(scrol);
			mid = Skin.librarys.getSkin(_skinID,"mid") as ImgSkinLoader;
			scroller.addChild(mid);
			bar.addChild(Skin.librarys.getSkin(_skinID,"bar"));
			arrowUp.x = arrowDown.x = 1;
		}

		override public function init(target:UIComponent, styleSet:Object, Skin:Class):void {
			super.init(target, styleSet, Skin);
			tar = target as VScrollBar;
			tar.addChildAt(scroller, 0);
			tar.addChildAt(arrowUp, 0);
			tar.addChildAt(arrowDown, 0);
			tar.addChildAt(bar, 0);
			
			//遮罩
			var maskClass:Class = SkinLoader.getClassFromSkinFile(SourceSkinLinkDefine.SCROLL_BAR_BACKGROUND_MASK);
		}

		private function showLater():void {
			lastY = -70;
		}

		override public function reDraw():void {
			var width:Number;
			var height:Number;
			width = tar.compoWidth;
			height = width;
			arrowDown.y = int(tar.compoHeight - tar.compoWidth + height) - arrowDown.height;
			if (scroller.y == 0 && arrowUp.visible == true){
				scroller.y = tar.compoWidth;
			}
			bar.height = tar.compoHeight;
			bar.width = tar.compoWidth;
			width = tar.compoWidth - 3;
			var lessNum:Number;
			if (arrowUp.visible == true){
				lessNum = tar.compoWidth;
			} else {
				lessNum = 0;
			}
			var clipH:Number = (1 + tar.maxScrollPosition / tar.clipSize);
			if (clipH == 0)
				clipH = 1;
			height = (tar.compoHeight - lessNum * 2) / clipH;
			if (height < 20){
				height = 20;
			}
			if (isNaN(tar.maxScrollPosition) || tar.maxScrollPosition <= 0){
				scroller.visible = false;
				return;
			} else {
				scroller.visible = true;
			}
			scrol.height = height;
			//scroller.width = tar.compoWidth - 2;
			//scroller.x = 1;
			mid.x = (scrol.width - mid.width) / 2;
			mid.y = (scrol.height - mid.height) / 2;
			if (scrol.width < 10){
				mid.visible = false;
			} else {
				mid.visible = true;
			}
			lastY = -50;
			setTimeout(showLater, 100);
		}
	}
}